import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemUserApi } from '#/api';

import { getRoleOptions } from '#/api';
import { $t } from '#/locales';

export function useFormSchema(): VbenFormSchema[] {
  return [
    // 隐藏id字段，仅仅用来区分新增和编辑
    {
      component: 'Input',
      fieldName: 'id',
      label: $t('system.user.id'),
      dependencies: {
        triggerFields: ['id'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'realName',
      label: $t('system.user.realName'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.user.username'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'password',
      label: $t('system.user.password'),
      rules: 'required',
      dependencies: {
        show: (values) => {
          return !values.id;
        },
        rules: (values) => {
          return values.id ? null : 'required';
        },
        triggerFields: ['id'],
      },
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: $t('system.user.phone'),
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: $t('system.user.email'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.user.status'),
    },
    {
      component: 'Textarea',
      fieldName: 'remark',
      label: $t('system.user.remark'),
    },
    {
      component: 'ApiSelect',
      fieldName: 'roles',
      label: $t('system.user.roles'),
      componentProps: {
        mode: 'multiple',
        style: { width: '100%' },
        placeholder: $t('ui.placeholder.select'),
        api: getRoleOptions,
        showSearch: true,
        allowClear: true,
        filterOption: (input: string, option: any) => {
          return option?.label?.toLowerCase().includes(input.toLowerCase());
        },
      },
      dependencies: {
        triggerFields: ['roles'],
        trigger: (values, formApi) => {
          const roleIds = values?.roles
            ?.map?.((item: { id: string }) => item?.id)
            .filter(Boolean);
          if (roleIds?.length > 0) {
            formApi.setFieldValue('roles', roleIds);
          }
        },
      },
      rules: 'selectRequired',
    },
    // {
    //   component: 'Input',
    //   fieldName: 'permissions',
    //   formItemClass: 'items-start',
    //   label: $t('system.role.title'),
    //   modelPropName: 'modelValue',
    // },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'realName',
      label: $t('system.user.realName'),
    },
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.user.username'),
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: $t('system.user.phone'),
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: $t('system.user.email'),
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
      },
      fieldName: 'status',
      label: $t('system.user.status'),
    },
    {
      component: 'ApiSelect',
      fieldName: 'roles',
      label: $t('system.user.roles'),
      componentProps: {
        placeholder: $t('ui.placeholder.select'),
        api: getRoleOptions,
        showSearch: true,
        allowClear: true,
        filterOption: (input: string, option: any) => {
          return option?.label?.toLowerCase().includes(input.toLowerCase());
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'remark',
      label: $t('system.user.remark'),
    },
  ];
}

export function useColumns<T = SystemUserApi.SystemUser>(
  onActionClick: OnActionClickFn<T>,
  onStatusChange?: (newStatus: any, row: T) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('system.user.id'),
      width: 100,
    },
    {
      field: 'realName',
      title: $t('system.user.realName'),
      width: 200,
    },
    {
      field: 'username',
      title: $t('system.user.username'),
      width: 200,
    },
    {
      field: 'phone',
      title: $t('system.user.phone'),
      width: 200,
    },
    {
      field: 'email',
      title: $t('system.user.email'),
      width: 200,
    },
    {
      field: 'roles',
      title: $t('system.user.roles'),
      width: 180,
      formatter: ({ row }) => {
        return row.roles.map((item: any) => item.name);
      },
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: onStatusChange ? 'CellSwitch' : 'CellTag',
      },
      field: 'status',
      title: $t('system.user.status'),
      width: 100,
    },
    {
      field: 'description',
      minWidth: 100,
      title: $t('system.user.remark'),
    },
    {
      field: 'createAt',
      title: $t('system.user.createTime'),
      width: 200,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'realName',
          nameTitle: $t('system.user.realName'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
      },
      field: 'operation',
      fixed: 'right',
      title: $t('system.user.operation'),
      width: 130,
    },
  ];
}
